#include "kernel_operator.h"
// tensor num for each queue
constexpr int32_t BUFFER_NUM = 2;

template<typename TYPE_X,typename TYPE_Y, typename TYPE_Z> class KernelAxpy {
    using T = TYPE_X;
public:
    __aicore__ inline KernelAxpy() {}
    __aicore__ inline void Init(GM_ADDR x,GM_ADDR y, GM_ADDR z, const AxpyCustomTilingData &tiling_data)
    {
        this->tiling=tiling_data;

        ASSERT(AscendC::GetBlockNum() != 0 && "block dim can not be zero!");
        uint32_t coreNum = AscendC::GetBlockIdx();
        uint32_t globalBufferIndex = tiling.bigCoreDataNum * AscendC::GetBlockIdx();
        if (coreNum < tiling.tailBlockNum) { 
          this->coreDataNum = tiling.bigCoreDataNum;
          this->tileNum = tiling.finalBigTileNum;
          this->tailDataNum = tiling.bigTailDataNum;
        }
        else { 
          this->coreDataNum = tiling.smallCoreDataNum;
          this->tileNum = tiling.finalSmallTileNum;
          this->tailDataNum = tiling.smallTailDataNum;
          globalBufferIndex -= (tiling.bigCoreDataNum - tiling.smallCoreDataNum) * (AscendC::GetBlockIdx() - tiling.tailBlockNum);
        }
        xGm.SetGlobalBuffer((__gm__ TYPE_X*)x + globalBufferIndex, this->coreDataNum);
        yGm.SetGlobalBuffer((__gm__ TYPE_Y*)y + globalBufferIndex, this->coreDataNum);
        zGm.SetGlobalBuffer((__gm__ TYPE_Z*)z + globalBufferIndex, this->coreDataNum);
        pipe.InitBuffer(inQueueX, BUFFER_NUM, tiling.tileDataNum * sizeof(TYPE_X));
        pipe.InitBuffer(inQueueY, BUFFER_NUM, tiling.tileDataNum * sizeof(TYPE_Y));
        pipe.InitBuffer(outQueueZ, BUFFER_NUM, tiling.tileDataNum * sizeof(TYPE_Z));
        // pipe.InitBuffer(tmpBuffer0, tiling.tileDataNum * sizeof(TYPE_X));
    }
    __aicore__ inline void Process()
    {
        int32_t loopCount = this->tileNum;
        this->processDataNum = tiling.tileDataNum;
        for (int32_t i = 0; i < loopCount; i++) {
            if (i == this->tileNum - 1) {
              this->processDataNum = this->tailDataNum;
            }
            CopyIn(i);
            Compute(i);
            CopyOut(i);
        }
    }

private:
    __aicore__ inline void CopyIn(int32_t progress)
    {
      AscendC::LocalTensor<TYPE_X> xLocal = inQueueX.AllocTensor<TYPE_X>();
      AscendC::LocalTensor<TYPE_Y> yLocal = inQueueY.AllocTensor<TYPE_Y>();
      AscendC::DataCopy(xLocal, xGm[progress * tiling.tileDataNum], this->processDataNum);
      AscendC::DataCopy(yLocal, yGm[progress * tiling.tileDataNum], this->processDataNum);
      inQueueX.EnQue(xLocal);
      inQueueY.EnQue(yLocal);
    }
    __aicore__ inline void Compute(int32_t progress)
    {
      AscendC::LocalTensor<TYPE_X> xLocal = inQueueX.DeQue<TYPE_X>();
      AscendC::LocalTensor<TYPE_Y> yLocal = inQueueY.DeQue<TYPE_Y>();
      AscendC::LocalTensor<TYPE_Z> zLocal = outQueueZ.AllocTensor<TYPE_Z>();
      // AscendC::LocalTensor<TYPE_X> tmpLocal = tmpBuffer0.Get<TYPE_X>();
      
      AscendC::Muls(xLocal, xLocal, (TYPE_X)tiling.a, this->processDataNum);
      AscendC::Add(zLocal, xLocal, yLocal, this->processDataNum);
      
      outQueueZ.EnQue<TYPE_Z>(zLocal);
      inQueueX.FreeTensor(xLocal);
      inQueueX.FreeTensor(yLocal);
    }
    __aicore__ inline void CopyOut(int32_t progress)
    {
      AscendC::LocalTensor<TYPE_Z> zLocal = outQueueZ.DeQue<TYPE_Z>();  
      AscendC::DataCopy(zGm[progress * tiling.tileDataNum], zLocal, this->processDataNum);
      outQueueZ.FreeTensor(zLocal);
    }

private:
    AscendC::TPipe pipe;
    AscendC::TQue<AscendC::QuePosition::VECIN, BUFFER_NUM> inQueueX,inQueueY;
    AscendC::TQue<AscendC::QuePosition::VECOUT, BUFFER_NUM> outQueueZ;
    // AscendC::TBuf<AscendC::QuePosition::VECCALC> tmpBuffer0;
    AscendC::GlobalTensor<TYPE_X> xGm;
    AscendC::GlobalTensor<TYPE_Y> yGm;
    AscendC::GlobalTensor<TYPE_Z> zGm;
    uint32_t coreDataNum;
    uint32_t tileNum;
    uint32_t tailDataNum;
    uint32_t processDataNum;
    AxpyCustomTilingData tiling;

};
extern "C" __global__ __aicore__ void axpy_custom(GM_ADDR x, GM_ADDR y,GM_ADDR z, GM_ADDR workspace, GM_ADDR tiling) {
    GET_TILING_DATA(tiling_data, tiling);
    KernelAxpy<DTYPE_X,DTYPE_Y, DTYPE_Z> op;
    op.Init(x, y,z, tiling_data);  
    op.Process();
}
